package com.service.impl;

import com.dao.BookMapper;
import com.dao.BrowRecoredMapper;
import com.dao.OrderRecoredMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.Book;
import com.model.BrowRecored;
import com.model.OrderRecored;
import com.service.OrderMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author : 虎虎
 * @date :2020/8/3 15:38
 */
@Service
public class OrderMapperServiceImpl implements OrderMapperService {
    @Autowired
    private OrderRecoredMapper orderRecoredMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BrowRecoredMapper browRecoredMapper;
    @Override
    public int orderBook(OrderRecored order) {
        order.setOrderDate(new Date());
        return orderRecoredMapper.insert(order);
    }

    @Override
    public HashMap<String, Object> selectAll(String limit, String page) {
        HashMap<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<OrderRecored> recoredList = orderRecoredMapper.selectAll();
        PageInfo<OrderRecored> pageInfo = new PageInfo<>(recoredList);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public List<String> selectByUserId(Integer uId,String flag) {
        //得到用户预定的书籍Id；
        //注意：这里预定的书籍不止一本，应该返回一个集合
        List<OrderRecored> recoredList = orderRecoredMapper.selectByUserId(uId);
        List<String> list = new ArrayList<>();
        if (recoredList.size()!=0){
            for (OrderRecored recored : recoredList) {
                Integer bookId = recored.getBookId();
                //查询库存
                Book book = bookMapper.selectByPrimaryKey(bookId);
                //判断该书的库存大于0？并将此集合的数据转移到借阅申请表中
                if(book.getStockNumber()>0){
                    //(1).先得到预定书籍库存不为0的预定信息
                    if(flag.equals("yes")){
                        //(2).删除这条信息
                        orderRecoredMapper.deleteByBNameUId(uId,bookId);
                        //(3).将这条信息转移到借阅申请表中
                        BrowRecored b = new BrowRecored();
                        b.setBookId(bookId);
                        b.setUserId(uId);
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                        String date=simpleDateFormat.format(new Date());
                        b.setBrowDate(date);
                        b.setBookState(0);
                        browRecoredMapper.insertSelective(b);
                    }
                    list.add(book.getBookName());
                }
            }
            return list;
        }
        return list;
    }

    @Override
    public HashMap<String, Object> selectOrderByUserId(Integer id, String limit, String page) {
        HashMap<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<OrderRecored> recoredList = orderRecoredMapper.selectOrderByUserId(id);
        PageInfo<OrderRecored> pageInfo = new PageInfo<>(recoredList);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public int delOrderByOrderId(int parseInt) {
        return orderRecoredMapper.deleteByPrimaryKey(parseInt);
    }
}